﻿//    Copyright 2010,2011 Himanshu Vasishth
//   
//    This file is part of SecretVault.
//
//    SecretVault is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    SecretVault is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with SecretVault.  If not, see <http://www.gnu.org/licenses/>.
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PasswordStoreProvider;
using System.Diagnostics;

namespace PasswordLocker.Controls
{
    public delegate void LoginInfoEvent(LoginInfo loginInfoControl);

    public partial class LoginInfoListControl : UserControl
    {
        public LoginInfoListControl()
        {
            InitializeComponent();
            loginInfoTable.BackColor = Colors.LoginInfoBackground;
            BackColor = Colors.LoginInfoBackground;
            mainTableLayoutPanel.BackColor = Colors.LoginInfoBackground;
        }

        /// <summary>
        /// Updates the list of logins displayed by the control
        /// </summary>
        public IEnumerable<LoginInfo> LoginInfos
        {
            set
            {
                UpdateLoginInfoList(value);
            }
        }

        /// <summary>
        /// Displays the password for the specified loginInfo object
        /// </summary>
        /// <param name="loginInfo"></param>
        /// <param name="password"></param>
        public void ShowPassword(LoginInfo loginInfo, string password)
        {
            // Find the LoginInfoControl matching the object
            var loginInfoControl = GetLoginInfoControl(loginInfo);
            if (loginInfoControl != null)
            {
                loginInfoControl.ShowPassword(password);
            }
        }

        /// <summary>
        /// Returns the LoginInfoControl being used to displaye the specified LoginInfo
        /// </summary>
        /// <param name="loginInfo"></param>
        private LoginInfoControl GetLoginInfoControl(LoginInfo loginInfo)
        {
            foreach(LoginInfoControl infoControl in loginInfoTable.Controls)
            {
                if(infoControl.LoginInfo.Equals(loginInfo))
                {
                    return infoControl;
                }
            }
            return null;
        }

        public event LoginInfoEvent ShowPasswordClicked;
        public event LoginInfoEvent DeleteClicked;
        public event LoginInfoEvent ChangeClicked;
        public event EventHandler AddClicked;

        private void UpdateLoginInfoList(IEnumerable<LoginInfo> loginInfos)
        {
            SuspendLayout();

            // clear  the existing controls
            loginInfoTable.Controls.Clear();
            loginInfoTable.RowStyles.Clear();
            foreach (var loginInfo in loginInfos)
            {
                loginInfoTable.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                var loginInfoControl = new LoginInfoControl
                                           {
                                               LoginInfo = loginInfo
                                           };
                loginInfoControl.ShowPasswordClicked += LoginInfoControl_ShowPasswordClicked;
                loginInfoControl.DeleteInfoClicked += LoginInfoControl_DeleteInfoClicked;
                loginInfoControl.ChangeInfoClicked += LoginInfoControl_ChangeInfoButtonClicked;
                loginInfoControl.Dock = DockStyle.Fill;
                loginInfoControl.Margin = new Padding(0);
                loginInfoControl.Padding = new Padding(0);
                loginInfoTable.Controls.Add(loginInfoControl);
            }

            // Add a last row to fill up the remaining space in the control
            loginInfoTable.RowStyles.Add(new RowStyle(SizeType.Percent, 100));

            ResumeLayout();
        }

        /// <summary>
        /// Invoked when Change button for a login info is clicked
        /// </summary>
        void LoginInfoControl_ChangeInfoButtonClicked(LoginInfo loginInfo)
        {
            if(ChangeClicked != null)
            {
                ChangeClicked(loginInfo);
            }
        }

        void LoginInfoControl_DeleteInfoClicked(LoginInfo loginInfo)
        {
            if(DeleteClicked != null)
            {
                DeleteClicked(loginInfo);
            }
        }

        void LoginInfoControl_ShowPasswordClicked(LoginInfo loginInfo)
        {
            if(ShowPasswordClicked != null)
            {
                ShowPasswordClicked(loginInfo);
            }
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            if(AddClicked != null)
            {
                AddClicked(this, e);
            }
        }
    }
}